package com.example.demo;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;


import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;

import com.example.demo.executeSql.*;
import com.example.demo.data.User;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.io.IOException;
import java.util.Enumeration;

@Controller
public class dealLogin {
    @GetMapping("/login")
    public String Login(HttpServletRequest request) throws IOException, ServletException {
        /*
        System.out.println("request.getContextPath():"+request.getContextPath());
        System.out.println("request.getHeaderNames():");
        Enumeration e = request.getHeaderNames();
        while(e.hasMoreElements()){
            Object o = e.nextElement();
            System.out.println((String)o+' '+request.getHeader((String)o));
        }
        System.out.println("request.getMethod():"+request.getMethod());

        //System.out.println("request.getParts():"+request.getParts());

        System.out.println("request.getPathInfo():"+request.getPathInfo());

        System.out.println("request.getPathTranslated():"+request.getPathTranslated());

        System.out.println("request.getQueryString():"+request.getQueryString());

        System.out.println("request.getRemoteUser():"+request.getRemoteUser());
        System.out.println("request.getRequestedSessionId():"+request.getRequestedSessionId());
        System.out.println("request.getRequestURI():"+request.getRequestURI());
        System.out.println("request.getRequestURL():"+request.getRequestURL());
        System.out.println("request.getServletPath():"+request.getServletPath());
        System.out.println("request.getSession():"+request.getSession());
        System.out.println("request.getUserPrincipal():"+request.getUserPrincipal());


        System.out.println("request.getLocalAddr():"+request.getLocalAddr());

        System.out.println("request.getLocalName():"+request.getLocalName());

        System.out.println("request.getLocalPort():"+request.getLocalPort());

        Enumeration e2 = request.getParameterNames();
        while(e2.hasMoreElements()){
            Object o = e2.nextElement();
            System.out.println((String)o+' '+request.getParameter((String)o));
        }


        System.out.println("request.getRemoteAddr():"+request.getRemoteAddr());

        System.out.println("request.getRemoteHost():"+request.getRemoteHost());

        System.out.println("request.getRemotePort():"+request.getRemotePort());

        System.out.println("request.getServerName():"+request.getServerName());

        System.out.println("request.getServerPort():"+request.getServerPort());

        System.out.println("request.getProtocol():"+request.getProtocol());
*/
        if (request.getSession().getAttribute("username") != null){
            System.out.println(request.getSession().getAttribute("username"));
            System.out.println(request.getSession().getAttribute("password"));
        }
        String username=request.getParameter("username");
        String password=request.getParameter("password");
        request.getSession().setAttribute("username",username);
        request.getSession().setAttribute("password",password);
        System.out.println(username);
        System.out.println(password);

        executeSql es = new executeSql();
        String id = es.selectUserAndPassword(username,password);
        if(id != null){
            request.getSession().setAttribute("user_id",id);
            return "index";
        }

        return "main";
    }

    @RequestMapping(value="/getUserId.do")
    @ResponseBody
    public User testJson(@RequestBody User user){
        /*
        System.out.println(user.getUsername());
        System.out.println(user.getPassword());
        user.setUsername("小明");
        user.setPassword("123456");*/

        executeSql es = new executeSql();
        String userid = es.selectUserAndPassword(user.getUsername(),user.getPassword());
        if(userid  == null)
            user.setUserid(0);
        else
            user.setUserid(Integer.parseInt(userid));
        return user;
    }




}
